{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b0162093-f429-4ffd-b4f0-bb70f2c8b689",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    },
    "tags": []
   },
   "source": [
    "## 基于高德地图地理编码与逆地理编码API的数据地点层次化处理\n",
    "API文档：https://lbs.amap.com/api/webservice/guide/api/georegeo"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a4f259e1-1d7b-4c9d-9af4-1fab40ab88ef",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import urllib\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "59357b9d-4bb4-459e-b4f1-5baf54393e7b",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "path = 'C:/Users/new/Desktop/疫情演化系统/数据/患者数据.xlsx'\n",
    "data = pd.read_excel(path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "1b0e7c79-b7c3-4491-98e9-82cb38c72e95",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>患者序号</th>\n",
       "      <th>性别</th>\n",
       "      <th>年龄</th>\n",
       "      <th>职业</th>\n",
       "      <th>家庭住址</th>\n",
       "      <th>分型</th>\n",
       "      <th>来源1</th>\n",
       "      <th>来源2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>女</td>\n",
       "      <td>29</td>\n",
       "      <td>托辅机构工作人员</td>\n",
       "      <td>南开区华苑新城</td>\n",
       "      <td>轻型</td>\n",
       "      <td>https://www.tj.gov.cn/sy/ztzl/tjsfyyqfkzt/yqtb...</td>\n",
       "      <td>https://weibo.com/ttarticle/p/show?id=23094047...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>女</td>\n",
       "      <td>10</td>\n",
       "      <td>咸水沽第七小学学生</td>\n",
       "      <td>津南区咸水沽镇</td>\n",
       "      <td>轻型</td>\n",
       "      <td>https://www.tj.gov.cn/sy/ztzl/tjsfyyqfkzt/yqtb...</td>\n",
       "      <td>https://weibo.com/ttarticle/p/show?id=23094047...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>男</td>\n",
       "      <td>10</td>\n",
       "      <td>咸水沽第七小学学生</td>\n",
       "      <td>津南区北闸口镇</td>\n",
       "      <td>轻型</td>\n",
       "      <td>https://www.tj.gov.cn/sy/ztzl/tjsfyyqfkzt/yqtb...</td>\n",
       "      <td>https://weibo.com/ttarticle/p/show?id=23094047...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>男</td>\n",
       "      <td>12</td>\n",
       "      <td>辛庄中学学生</td>\n",
       "      <td>津南区辛庄镇</td>\n",
       "      <td>轻型</td>\n",
       "      <td>https://www.tj.gov.cn/sy/ztzl/tjsfyyqfkzt/yqtb...</td>\n",
       "      <td>https://weibo.com/ttarticle/p/show?id=23094047...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>男</td>\n",
       "      <td>13</td>\n",
       "      <td>辛庄中学学生</td>\n",
       "      <td>津南区辛庄镇</td>\n",
       "      <td>轻型</td>\n",
       "      <td>https://www.tj.gov.cn/sy/ztzl/tjsfyyqfkzt/yqtb...</td>\n",
       "      <td>https://weibo.com/ttarticle/p/show?id=23094047...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   患者序号 性别  年龄         职业     家庭住址  分型  \\\n",
       "0     1  女  29   托辅机构工作人员  南开区华苑新城  轻型   \n",
       "1     2  女  10  咸水沽第七小学学生  津南区咸水沽镇  轻型   \n",
       "2     3  男  10  咸水沽第七小学学生  津南区北闸口镇  轻型   \n",
       "3     4  男  12     辛庄中学学生   津南区辛庄镇  轻型   \n",
       "4     5  男  13     辛庄中学学生   津南区辛庄镇  轻型   \n",
       "\n",
       "                                                 来源1  \\\n",
       "0  https://www.tj.gov.cn/sy/ztzl/tjsfyyqfkzt/yqtb...   \n",
       "1  https://www.tj.gov.cn/sy/ztzl/tjsfyyqfkzt/yqtb...   \n",
       "2  https://www.tj.gov.cn/sy/ztzl/tjsfyyqfkzt/yqtb...   \n",
       "3  https://www.tj.gov.cn/sy/ztzl/tjsfyyqfkzt/yqtb...   \n",
       "4  https://www.tj.gov.cn/sy/ztzl/tjsfyyqfkzt/yqtb...   \n",
       "\n",
       "                                                 来源2  \n",
       "0  https://weibo.com/ttarticle/p/show?id=23094047...  \n",
       "1  https://weibo.com/ttarticle/p/show?id=23094047...  \n",
       "2  https://weibo.com/ttarticle/p/show?id=23094047...  \n",
       "3  https://weibo.com/ttarticle/p/show?id=23094047...  \n",
       "4  https://weibo.com/ttarticle/p/show?id=23094047...  "
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "1213a625-20f5-4810-93bc-6bbf5fe3441b",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "data['完整地址'] = ''\n",
    "data['所在区'] = ''\n",
    "data['街道'] = ''\n",
    "data['township'] = ''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "9ae339f2-2342-400c-89bb-bb11791a3102",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>患者序号</th>\n",
       "      <th>性别</th>\n",
       "      <th>年龄</th>\n",
       "      <th>职业</th>\n",
       "      <th>家庭住址</th>\n",
       "      <th>分型</th>\n",
       "      <th>来源1</th>\n",
       "      <th>来源2</th>\n",
       "      <th>完整地址</th>\n",
       "      <th>所在区</th>\n",
       "      <th>街道</th>\n",
       "      <th>township</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>女</td>\n",
       "      <td>29</td>\n",
       "      <td>托辅机构工作人员</td>\n",
       "      <td>南开区华苑新城</td>\n",
       "      <td>轻型</td>\n",
       "      <td>https://www.tj.gov.cn/sy/ztzl/tjsfyyqfkzt/yqtb...</td>\n",
       "      <td>https://weibo.com/ttarticle/p/show?id=23094047...</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>女</td>\n",
       "      <td>10</td>\n",
       "      <td>咸水沽第七小学学生</td>\n",
       "      <td>津南区咸水沽镇</td>\n",
       "      <td>轻型</td>\n",
       "      <td>https://www.tj.gov.cn/sy/ztzl/tjsfyyqfkzt/yqtb...</td>\n",
       "      <td>https://weibo.com/ttarticle/p/show?id=23094047...</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>男</td>\n",
       "      <td>10</td>\n",
       "      <td>咸水沽第七小学学生</td>\n",
       "      <td>津南区北闸口镇</td>\n",
       "      <td>轻型</td>\n",
       "      <td>https://www.tj.gov.cn/sy/ztzl/tjsfyyqfkzt/yqtb...</td>\n",
       "      <td>https://weibo.com/ttarticle/p/show?id=23094047...</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>男</td>\n",
       "      <td>12</td>\n",
       "      <td>辛庄中学学生</td>\n",
       "      <td>津南区辛庄镇</td>\n",
       "      <td>轻型</td>\n",
       "      <td>https://www.tj.gov.cn/sy/ztzl/tjsfyyqfkzt/yqtb...</td>\n",
       "      <td>https://weibo.com/ttarticle/p/show?id=23094047...</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>男</td>\n",
       "      <td>13</td>\n",
       "      <td>辛庄中学学生</td>\n",
       "      <td>津南区辛庄镇</td>\n",
       "      <td>轻型</td>\n",
       "      <td>https://www.tj.gov.cn/sy/ztzl/tjsfyyqfkzt/yqtb...</td>\n",
       "      <td>https://weibo.com/ttarticle/p/show?id=23094047...</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   患者序号 性别  年龄         职业     家庭住址  分型  \\\n",
       "0     1  女  29   托辅机构工作人员  南开区华苑新城  轻型   \n",
       "1     2  女  10  咸水沽第七小学学生  津南区咸水沽镇  轻型   \n",
       "2     3  男  10  咸水沽第七小学学生  津南区北闸口镇  轻型   \n",
       "3     4  男  12     辛庄中学学生   津南区辛庄镇  轻型   \n",
       "4     5  男  13     辛庄中学学生   津南区辛庄镇  轻型   \n",
       "\n",
       "                                                 来源1  \\\n",
       "0  https://www.tj.gov.cn/sy/ztzl/tjsfyyqfkzt/yqtb...   \n",
       "1  https://www.tj.gov.cn/sy/ztzl/tjsfyyqfkzt/yqtb...   \n",
       "2  https://www.tj.gov.cn/sy/ztzl/tjsfyyqfkzt/yqtb...   \n",
       "3  https://www.tj.gov.cn/sy/ztzl/tjsfyyqfkzt/yqtb...   \n",
       "4  https://www.tj.gov.cn/sy/ztzl/tjsfyyqfkzt/yqtb...   \n",
       "\n",
       "                                                 来源2 完整地址 所在区 街道 township  \n",
       "0  https://weibo.com/ttarticle/p/show?id=23094047...                       \n",
       "1  https://weibo.com/ttarticle/p/show?id=23094047...                       \n",
       "2  https://weibo.com/ttarticle/p/show?id=23094047...                       \n",
       "3  https://weibo.com/ttarticle/p/show?id=23094047...                       \n",
       "4  https://weibo.com/ttarticle/p/show?id=23094047...                       "
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "7ef9d881-53d6-44cd-b401-5deea8391c71",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "API_key = \"947c7f60f731b54bd6474ce18d5d894b\"  # 使用高德地图地理编码API所需的key,这里我申请的是个人key,每日只能查询5000条数据\n",
    "city = \"天津\"  # 默认对天津的地点进行地理编码和逆编码\n",
    "city = urllib.parse.quote(city)  # 这里要注意,按照标准,URL只允许一部分ASCII字符,其他字符（如汉字）是不符合标准的，此时就要进行编码,下方的address同样需要"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "96b743cc",
   "metadata": {
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "for index,row in data.iterrows():\n",
    "    address = row['家庭住址']\n",
    "    if address is not np.NaN:\n",
    "        address = urllib.parse.quote(address)\n",
    "        geo_url = \"https://restapi.amap.com/v3/geocode/geo?key={}&address={}&city={}\".format(API_key,address,city)\n",
    "\n",
    "        req = urllib.request.Request(geo_url)  # 将url对象转化为request对象\n",
    "        ret = urllib.request.urlopen(req)  # 发送请求，打开url，也可以直接传入url\n",
    "        result = ret.read().decode('utf-8')\n",
    "\n",
    "        result = eval(result)\n",
    "        if result['status'] == '1':  # 有时候API会有一些没法返回正常的查询结果，比如乐齐国际影城，不知道为啥返回不了正常结果，status=0\n",
    "            location = result['geocodes'][0]['location']\n",
    "            longitude, latitude = eval(location)  # 地点对应经纬度\n",
    "\n",
    "            regeo_url = 'https://restapi.amap.com/v3/geocode/regeo?key={}&location={}&extensions=base'.format(API_key,location)  # extensions 参数取值为 all 时会返回基本地址信息、附近 POI 内容、道路信息以及道路交叉口信息\n",
    "            req = urllib.request.Request(regeo_url)  # 将url对象转化为request对象\n",
    "            ret = urllib.request.urlopen(req)  # 发送请求，打开url，也可以直接传入url\n",
    "            result = ret.read().decode('utf-8')\n",
    "\n",
    "            result = eval(result)\n",
    "            if result['status'] == '1':\n",
    "                formatted_address = result['regeocode']['formatted_address']  # 完整地址\n",
    "                district = result['regeocode']['addressComponent']['district']  # 所在区\n",
    "                street = result['regeocode']['addressComponent']['streetNumber']['street']  # 所在街道\n",
    "                township = result['regeocode']['addressComponent']['township']  # 暂不了解此词条的含义\n",
    "                # formatted_address\n",
    "                # district\n",
    "                # street\n",
    "                # township\n",
    "                if formatted_address:  # 主要是防止有些地址返回的是空集\n",
    "                    data.loc[index,'完整地址'] = formatted_address\n",
    "                if district:\n",
    "                    data.loc[index,'所在区'] = district\n",
    "                if street:\n",
    "                    data.loc[index,'街道'] = street\n",
    "                if township:\n",
    "                    data.loc[index,'township'] = township\n",
    "                if longitude:\n",
    "                    data.loc[index,'经度'] = longitude\n",
    "                if latitude:\n",
    "                    data.loc[index,'纬度'] = latitude"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "a4a92216",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'%E6%B2%B3%E5%8C%97%E5%8C%BA%E9%B8%BF%E9%A1%BA%E9%87%8C%E8%A1%97'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "address"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "c328ba19",
   "metadata": {
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>患者序号</th>\n",
       "      <th>性别</th>\n",
       "      <th>年龄</th>\n",
       "      <th>职业</th>\n",
       "      <th>家庭住址</th>\n",
       "      <th>分型</th>\n",
       "      <th>来源1</th>\n",
       "      <th>来源2</th>\n",
       "      <th>完整地址</th>\n",
       "      <th>所在区</th>\n",
       "      <th>街道</th>\n",
       "      <th>township</th>\n",
       "      <th>经度</th>\n",
       "      <th>纬度</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>女</td>\n",
       "      <td>29</td>\n",
       "      <td>托辅机构工作人员</td>\n",
       "      <td>南开区华苑新城</td>\n",
       "      <td>轻型</td>\n",
       "      <td>https://www.tj.gov.cn/sy/ztzl/tjsfyyqfkzt/yqtb...</td>\n",
       "      <td>https://weibo.com/ttarticle/p/show?id=23094047...</td>\n",
       "      <td>天津市南开区华苑街道华苑新城</td>\n",
       "      <td>南开区</td>\n",
       "      <td>华苑路</td>\n",
       "      <td>华苑街道</td>\n",
       "      <td>117.143640</td>\n",
       "      <td>39.075703</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>女</td>\n",
       "      <td>10</td>\n",
       "      <td>咸水沽第七小学学生</td>\n",
       "      <td>津南区咸水沽镇</td>\n",
       "      <td>轻型</td>\n",
       "      <td>https://www.tj.gov.cn/sy/ztzl/tjsfyyqfkzt/yqtb...</td>\n",
       "      <td>https://weibo.com/ttarticle/p/show?id=23094047...</td>\n",
       "      <td>天津市津南区咸水沽镇咸水沽镇咸水沽镇人民政府</td>\n",
       "      <td>津南区</td>\n",
       "      <td>丰收路</td>\n",
       "      <td>咸水沽镇</td>\n",
       "      <td>117.395242</td>\n",
       "      <td>38.979148</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>男</td>\n",
       "      <td>10</td>\n",
       "      <td>咸水沽第七小学学生</td>\n",
       "      <td>津南区北闸口镇</td>\n",
       "      <td>轻型</td>\n",
       "      <td>https://www.tj.gov.cn/sy/ztzl/tjsfyyqfkzt/yqtb...</td>\n",
       "      <td>https://weibo.com/ttarticle/p/show?id=23094047...</td>\n",
       "      <td>天津市津南区北闸口镇政安里北闸口镇人民政府</td>\n",
       "      <td>津南区</td>\n",
       "      <td>北营路</td>\n",
       "      <td>北闸口镇</td>\n",
       "      <td>117.406017</td>\n",
       "      <td>38.950556</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>男</td>\n",
       "      <td>12</td>\n",
       "      <td>辛庄中学学生</td>\n",
       "      <td>津南区辛庄镇</td>\n",
       "      <td>轻型</td>\n",
       "      <td>https://www.tj.gov.cn/sy/ztzl/tjsfyyqfkzt/yqtb...</td>\n",
       "      <td>https://weibo.com/ttarticle/p/show?id=23094047...</td>\n",
       "      <td>天津市津南区辛庄镇龙汇公寓辛庄镇人民政府</td>\n",
       "      <td>津南区</td>\n",
       "      <td>平凡道</td>\n",
       "      <td>辛庄镇</td>\n",
       "      <td>117.350345</td>\n",
       "      <td>39.026690</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>男</td>\n",
       "      <td>13</td>\n",
       "      <td>辛庄中学学生</td>\n",
       "      <td>津南区辛庄镇</td>\n",
       "      <td>轻型</td>\n",
       "      <td>https://www.tj.gov.cn/sy/ztzl/tjsfyyqfkzt/yqtb...</td>\n",
       "      <td>https://weibo.com/ttarticle/p/show?id=23094047...</td>\n",
       "      <td>天津市津南区辛庄镇龙汇公寓辛庄镇人民政府</td>\n",
       "      <td>津南区</td>\n",
       "      <td>平凡道</td>\n",
       "      <td>辛庄镇</td>\n",
       "      <td>117.350345</td>\n",
       "      <td>39.026690</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   患者序号 性别  年龄         职业     家庭住址  分型  \\\n",
       "0     1  女  29   托辅机构工作人员  南开区华苑新城  轻型   \n",
       "1     2  女  10  咸水沽第七小学学生  津南区咸水沽镇  轻型   \n",
       "2     3  男  10  咸水沽第七小学学生  津南区北闸口镇  轻型   \n",
       "3     4  男  12     辛庄中学学生   津南区辛庄镇  轻型   \n",
       "4     5  男  13     辛庄中学学生   津南区辛庄镇  轻型   \n",
       "\n",
       "                                                 来源1  \\\n",
       "0  https://www.tj.gov.cn/sy/ztzl/tjsfyyqfkzt/yqtb...   \n",
       "1  https://www.tj.gov.cn/sy/ztzl/tjsfyyqfkzt/yqtb...   \n",
       "2  https://www.tj.gov.cn/sy/ztzl/tjsfyyqfkzt/yqtb...   \n",
       "3  https://www.tj.gov.cn/sy/ztzl/tjsfyyqfkzt/yqtb...   \n",
       "4  https://www.tj.gov.cn/sy/ztzl/tjsfyyqfkzt/yqtb...   \n",
       "\n",
       "                                                 来源2                    完整地址  \\\n",
       "0  https://weibo.com/ttarticle/p/show?id=23094047...          天津市南开区华苑街道华苑新城   \n",
       "1  https://weibo.com/ttarticle/p/show?id=23094047...  天津市津南区咸水沽镇咸水沽镇咸水沽镇人民政府   \n",
       "2  https://weibo.com/ttarticle/p/show?id=23094047...   天津市津南区北闸口镇政安里北闸口镇人民政府   \n",
       "3  https://weibo.com/ttarticle/p/show?id=23094047...    天津市津南区辛庄镇龙汇公寓辛庄镇人民政府   \n",
       "4  https://weibo.com/ttarticle/p/show?id=23094047...    天津市津南区辛庄镇龙汇公寓辛庄镇人民政府   \n",
       "\n",
       "   所在区   街道 township          经度         纬度  \n",
       "0  南开区  华苑路     华苑街道  117.143640  39.075703  \n",
       "1  津南区  丰收路     咸水沽镇  117.395242  38.979148  \n",
       "2  津南区  北营路     北闸口镇  117.406017  38.950556  \n",
       "3  津南区  平凡道      辛庄镇  117.350345  39.026690  \n",
       "4  津南区  平凡道      辛庄镇  117.350345  39.026690  "
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "fafc99d1-c505-4546-b239-f2e2f3e0a621",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "outputpath='C:/Users/new/Desktop/Geocoding.csv'\n",
    "data.to_csv(outputpath,sep=',',index=False,header=True,encoding='utf_8_sig')  # 保存结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dbdd1b65-dc21-43f3-a350-c0472e0a3a02",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
